package com.cn.wanxi.message.controller;

import com.cn.wanxi.message.domain.MessageModel;
import com.cn.wanxi.message.domain.Result;
import com.cn.wanxi.message.socketserver.WebSocketServer;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

/**
 * @ClassName MessageController
 * @Description TODO
 * @Author JiJiang
 * @Date 2022/10/15 11:18
 * @Version 1.0
 */

@RestController
@CrossOrigin
@RequestMapping("/message")
public class MessageController {


    @PostMapping
    public Result userSendMsgToUser(MessageModel msgModel) {

        WebSocketServer publishSocket = WebSocketServer.getSocketByUserName(msgModel.getPublisher());

        if (Objects.isNull(publishSocket)) {
            return Result.getResult("error-publisher has no map socket");
        }

        boolean sendResult = publishSocket.sendMessageToUser(msgModel);
        return Result.getResult(sendResult ? "success" : "error-receiver has no map socket");

    }

}
